package cn.edu.ctbu.datajpa01.webapi;


import cn.edu.ctbu.datajpa01.core.PageUtils;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {

    @Autowired
    private StudentService studentService;

    /**
     * 读取的uil：/webapi/student/list
     *
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll() {
        List<Student> students = studentService.findAll();
        return students;
    }

    /**
     * 读取的uil：/webapi/student/get/id
     *
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id) {
        Student student = studentService.findById(id);
        student.setPassword("");
        return student;
    }

    /**
     * 新增学生的方法,/webapi/student/insert
     *
     * @return
     */
    @PostMapping("/insert")
    public Student insert(Student student) {
        Student student1 = studentService.insert(student);
        return student1;
    }

    /**
     * 更新学生的方法,/webapi/student/insert
     *
     * @return
     */
    @PutMapping("/update")
    public Student update(Student student) {
//读取旧密码
        Student oldstudent = studentService.findById(student.getId());
        if (StringUtils.isEmpty(student.getPassword())) {
//            没有新密码，那么就是旧密码
            student.setPassword(oldstudent.getPassword());
        }
        student = studentService.update(student);
        return student;
    }

    /**
     * 删除学生的方法,/webapi/student/delete
     *
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        studentService.delete(id);

    }

    /**
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value="page",defaultValue="0")Integer page,
                               @RequestParam(value="size",defaultValue="10")Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name ){
        Page<Student> studentPage;
        if(StringUtils.isEmpty(name)){
            Pageable pageable = PageRequest.of(page, size, Sort.by("id").descending());
            studentPage = studentService.findAll(pageable);
        }else{
            Student student=new Student();
            student.setName(name);
            Pageable pageable=PageRequest.of(0,10,Sort.by("id").descending());
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example=Example.of(student,matcher);

            studentPage=studentService.findAll(example,pageable);


        }

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return pageUtils;
    }



    /**
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getbypage2")
    public PageUtils getByPage2(@RequestParam(value="page",defaultValue="0")Integer page,
                               @RequestParam(value="size",defaultValue="10")Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name ,
                                @RequestParam(value = "sex",defaultValue = "")String sex ){
        Page<Student> studentPage;
        if(StringUtils.isEmpty(name)&&StringUtils.isEmpty(sex)){
            Pageable pageable = PageRequest.of(page, size, Sort.by("id").descending());
            studentPage = studentService.findAll(pageable);
        }else if(StringUtils.isEmpty(name)&&!StringUtils.isEmpty(sex)){
            Student student=new Student();
            Integer num = Integer.valueOf(sex);
            student.setSex(num);

            Pageable pageable=PageRequest.of(0,10,Sort.by("id").descending());
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("sex",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example=Example.of(student,matcher);

            studentPage=studentService.findAll(example,pageable);
        }
        else if(!StringUtils.isEmpty(name)&&StringUtils.isEmpty(sex)){
            Student student=new Student();
            student.setName(name);

            Pageable pageable=PageRequest.of(0,10,Sort.by("id").descending());
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example=Example.of(student,matcher);

            studentPage=studentService.findAll(example,pageable);
        }
        else{
            Student student=new Student();
            student.setName(name);
            Integer num = Integer.valueOf(sex);
            student.setSex(num);

            Pageable pageable=PageRequest.of(0,10,Sort.by("id").descending());
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example=Example.of(student,matcher);

            studentPage=studentService.findAll(example,pageable);


        }
        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return pageUtils;
    }

}
